﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AdminModule.IndexMarkServiceReference;
using System.Collections.ObjectModel;
using AdminModule.CommonClasses;

namespace AdminModule.StructEditStratagies
{
    public class UniversityStructEditStrategy : StructEditBaseStrategy
    {
        private int IDTerm;

        public UniversityStructEditStrategy(int IDTerm)
        {
            this.IDTerm = IDTerm;
        }

        public override bool Move(DTOBase obj, DTOBase fromObject, DTOBase toObject)
        {
            if(obj is AdmDepartment && fromObject is AdmDepartment && toObject is AdmDepartment)
            {
                AdmDepartment department = obj as AdmDepartment;
                AdmDepartment toDepartment = toObject as AdmDepartment;
                AdmDepartment fromDepartment = fromObject as AdmDepartment;

                toDepartment.Childs.Add(department);
                fromDepartment.Childs.Remove(department);

                department.ParentDepartment = toDepartment;

                this.SetState(department, StateObject.Updated);
                return true;
            }
            if (obj is AdmRespondent && fromObject is AdmDepartment && toObject is AdmDepartment)
            {
                AdmRespondent respondent = obj as AdmRespondent;
                AdmDepartment toDepartment = toObject as AdmDepartment;
                AdmDepartment fromDepartment = fromObject as AdmDepartment;

                toDepartment.Childs.Add(respondent);
                fromDepartment.Childs.Remove(respondent);

                respondent.Department = toDepartment;

                this.SetState(respondent, StateObject.Updated);
                return true;
            }
            if (obj is AdmDepartment && fromObject is AdmDepartment && toObject is RootTreeView)
            {
                AdmDepartment department = obj as AdmDepartment;
                RootTreeView root = toObject as RootTreeView;
                AdmDepartment fromDepartment = fromObject as AdmDepartment;

                root.Childs.Add(department);
                fromDepartment.Childs.Remove(department);

                department.ParentDepartment = null;

                this.SetState(department, StateObject.Updated);
                return true;
            }
            return false;
        }

        public override bool Delete(DTOBase obj, DTOBase fromObject)
        {
            if (obj is AdmDepartment && fromObject is AdmDepartment)
            {
                AdmDepartment department = obj as AdmDepartment;
                AdmDepartment fromDepartment = fromObject as AdmDepartment;

                fromDepartment.Childs.Remove(department);

                this.SetState(department, StateObject.Deleted);
                return true;
            }
            if (obj is AdmDepartment && fromObject is RootTreeView)
            {
                AdmDepartment department = obj as AdmDepartment;
                RootTreeView root = fromObject as RootTreeView;

                root.Childs.Remove(department);

                this.SetState(department, StateObject.Deleted);
                return true;
            }
            if (obj is AdmRespondent && fromObject is AdmDepartment)
            {
                AdmRespondent respondent = obj as AdmRespondent;
                AdmDepartment fromDepartment = fromObject as AdmDepartment;

                fromDepartment.Childs.Remove(respondent);

                this.SetState(respondent, StateObject.Deleted);
                return true;
            }
            return false;
        }

        public override bool Add(DTOBase obj, DTOBase toObject)
        {
            if (obj is AdmDepartment && toObject is AdmDepartment)
            {
                AdmDepartment toDepartment = toObject as AdmDepartment;
                AdmDepartment department = obj as AdmDepartment;
                department.IDTerm = this.IDTerm;
                department.ParentDepartment = toDepartment;

                toDepartment.Childs.AddToForward(department);

                this.SetState(department, StateObject.Created);

                return true;
            }
            if (obj is AdmDepartment && toObject is RootTreeView)
            {
                RootTreeView root = toObject as RootTreeView;
                AdmDepartment department = obj as AdmDepartment;
                department.IDTerm = this.IDTerm;
                department.ParentDepartment = null;

                root.Childs.AddToForward(department);

                this.SetState(department, StateObject.Created);

                return true;
            }
            if (obj is AdmPerson && toObject is AdmDepartment)
            {
                AdmPerson person = obj as AdmPerson;
                AdmDepartment toDepartment = toObject as AdmDepartment;
                AdmRespondent respondent = new AdmRespondent
                {
                    Person = person,
                    IDTerm = this.IDTerm,
                    Department = toDepartment
                };

                toDepartment.Childs.AddToForward(respondent);

                this.SetState(respondent, StateObject.Created);
                return true;
            }
            if (obj is AdmRespondent && toObject is AdmDepartment)
            {
                AdmDepartment department = toObject as AdmDepartment;
                AdmRespondent respondent = obj as AdmRespondent;

                department.Childs.Add(respondent);
            }
            return false;
        }

        public override DTOBase CopyTo(DTOBase obj, DTOBase toObject, int toIDTerm)
        {
            if (obj is AdmDepartment && toObject is RootTreeView)
            {
                AdmDepartment department = obj as AdmDepartment;
                department.ParentDepartment = null;
                department.State = StateObject.Created;
                return department;
            }
            if (obj is AdmRespondent && toObject is AdmDepartment)
            {
                AdmRespondent respondent = obj as AdmRespondent;
                AdmDepartment toDepartment = toObject as AdmDepartment;
                respondent.Department = toDepartment;
                respondent.IDTerm = toIDTerm;
                toDepartment.State = StateObject.Created;
                return respondent;
            }
            if (obj is AdmDepartment && toObject is AdmDepartment)
            {
                AdmDepartment department = obj as AdmDepartment;
                AdmDepartment toDepartment = toObject as AdmDepartment;
                department.ParentDepartment = toDepartment;
                department.IDTerm = toIDTerm;
                department.State = StateObject.Created;
                return department;
            }
            return null;
        }

        public override void Replace(DTOBase new_obj, DTOBase old_obj, DTOBase parentObject)
        {
            if (new_obj is AdmDepartment && old_obj is AdmDepartment)
            {
                AdmDepartment new_department = new_obj as AdmDepartment;
                AdmDepartment old_department = old_obj as AdmDepartment;

                old_department.CopyFrom(new_department);
            }
            if (new_obj is AdmPerson && old_obj is AdmRespondent)
            {
                AdmPerson person = new_obj as AdmPerson;
                AdmRespondent respondent = old_obj as AdmRespondent;

                respondent.Person = person;
            }
        }
    }
}
